#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int fa[N];
int ch[N][2];
pair<int,int> val[N];
pair<int,int> mi[N];
pair<int,int> ma[N];
struct edge{
    int u,v,a,b;
};
bool rtag[N];
void rev(int x){
    rtag[x] ^= 1;
    swap(ch[x][0],ch[x][1]);
}
void push_down(int now){
    if(rtag[now]){
        rev(ch[now][0]);
        rev(ch[now][1]);
        rtag[now] = 0;
        return;
    }
}
void push_up(int now){
    ma[now] = mi[now] = val[now];
    if(ch[now][0]){
        ma[now] = max(ma[now],ma[ch[now][0]]);
        mi[now] = min(mi[now],mi[ch[now][0]]);
    }
    if(ch[now][1]){
        ma[now] = max(ma[now],ma[ch[now][1]]);
        mi[now] = min(mi[now],mi[ch[now][1]]);
    }
}
bool is_root(int now){
    return ch[fa[now]][0] != now && ch[fa[now]][1] != now;
}
bool get(int x){
    return ch[fa[x]][1] == x;
}
void rotate(int x){
    int y = fa[x],z = fa[y];
    push_down(y);
    push_down(x);
    int p = get(x);
    if(!is_root(y)){
        ch[z][get(y)] = x;
    }
    fa[x] = z;
    fa[y] = x;
    fa[ch[x][p ^ 1]] = y;
    ch[y][p] = ch[x][p ^ 1];
    ch[x][p ^ 1] = y;
    push_up(y);
    push_up(x);
}
void splay(int x){
    while(!is_root(x)){
        int f = fa[x];
        if(!is_root(f)){
            rotate(get(x) == get(f)?f:x);
        }
        rotate(x);
    }
}
int access(int x){
    int p = 0;
    while(x){
        splay(x);
        push_down(x);
        ch[x][1] = p;
        push_up(x);
        p = x;
        x = fa[x];
    }
    return p;
}
void make_rot(int x){
    access(x);
    splay(x);
    rev(x);
}
void link(int u,int v){
    make_rot(u);
    fa[u] = v;
}
void split(int u,int v){
    make_rot(u);
    access(v);
    splay(v);
}
void cut(int u,int v){
    split(u,v);
    splay(u);
    push_down(u);
    ch[u][1] = 0;
    fa[v] = 0;
    push_up(u);
}
int find_rot(int u){
    access(u);
    splay(u);
    int p = u;
    push_down(p);
    while(ch[p][0]){
        p = ch[p][0];
        push_down(p);
    }
    splay(p);
    return p;
}
int main(){
    int n,m;
    cin>>n>>m;
    vector<edge> es;
    for(int i = 1;i <= m;i++){
        int u,v,a,b;
        cin>>u>>v>>a>>b;
        if(u == v)continue;
        es.push_back({u,v,a,b});
    }
    m = es.size();
    sort(es.begin(),es.end(),[](edge t1,edge t2){
        return t1.a < t2.a || t1.a == t2.a && t1.b < t2.b;
    });
    int res = 0x3f3f3f3f;
    for(int i = 1;i<=m;i++){
        //cerr<<es[i - 1].u<<' '<<es[i - 1].v<<' '<<es[i - 1].a<<' '<<es[i - 1].b<<'\n';
        val[i + n] = make_pair(es[i - 1].b,i);
        int u = es[i - 1].u,v = es[i - 1].v;
        make_rot(u);
        if(find_rot(v) != u){
            link(u,i + n);
            link(v,i + n);
        }else{
            access(v);
            splay(v);
            pair<int,int> g = ma[v];
            if(g.first > es[i - 1].b){
                cut(es[g.second - 1].u,g.second + n);
                cut(es[g.second - 1].v,g.second + n);
                link(u,i + n);
                link(v,i + n);
            }
        }
        make_rot(1);
        if(find_rot(n) == 1){
            split(1,n);
            splay(1);
            res = min(ma[1].first + es[i - 1].a,res);
        }
    }
    if(res == 0x3f3f3f3f){
        cout<<-1<<'\n';
    }else{
        cout<<res<<'\n';
    }
}